<template>
    <div class="login-container">
        <svg class="login-bg-svg" viewBox="0 0 1440 900" fill="none" xmlns="http://www.w3.org/2000/svg">
            <ellipse cx="1200" cy="200" rx="340" ry="120" fill="#f9a8d4" fill-opacity="0.18" />
            <ellipse cx="300" cy="800" rx="400" ry="140" fill="#fbcfe8" fill-opacity="0.13" />
            <ellipse cx="900" cy="700" rx="300" ry="100" fill="#ec4899" fill-opacity="0.08" />
        </svg>
        <div class="login-card">
            <h2 class="login-title">溯源系统登录</h2>
            <el-form :model="form" :rules="rules" ref="formRef" label-width="80px" class="login-form">
                <el-form-item label="用户名" prop="Name">
                    <el-input v-model="form.Name" placeholder="请输入用户名" clearable />
                </el-form-item>
                <el-form-item label="密码" prop="Pwd">
                    <el-input v-model="form.Pwd" type="password" placeholder="请输入密码" show-password clearable />
                </el-form-item>
                <el-form-item label="验证码" prop="captcha">
                    <el-row :gutter="8">
                        <el-col :span="14">
                            <el-input v-model="form.captcha" placeholder="请输入验证码" maxlength="6" clearable />
                        </el-col>
                        <el-col :span="10">
                            <div class="captcha-img" @click="refreshCaptcha" title="点击刷新验证码">{{ captchaCode }}</div>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item>
                    <el-checkbox v-model="rememberMe">记住密码</el-checkbox>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" class="login-btn" @click="onLogin" :loading="loading">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { axiosr } from '@/http/axiosRead'

const formRef = ref<FormInstance>()
const form = reactive({
    Name: '',
    Pwd: '',
    captcha: ''
})
const rules = reactive<FormRules>({
    Name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    Pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    captcha: [
        { required: true, message: '请输入验证码', trigger: 'blur' },
        { validator: validateCaptcha, trigger: 'blur' }
    ]
})
const loading = ref(false)
const captchaCode = ref(generateCaptcha())
const rememberMe = ref(false)

function generateCaptcha() {
    const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789'
    let code = ''
    for (let i = 0; i < 4; i++) {
        code += chars.charAt(Math.floor(Math.random() * chars.length))
    }
    return code
}
function refreshCaptcha() {
    captchaCode.value = generateCaptcha()
}
function validateCaptcha(rule: any, value: string, callback: any) {
    if (!value) {
        callback(new Error('请输入验证码'))
    } else if (value.toLowerCase() !== captchaCode.value.toLowerCase()) {
        callback(new Error('验证码错误'))
    } else {
        callback()
    }
}

onMounted(() => {
    const name = localStorage.getItem('rememberName')
    const pwd = localStorage.getItem('rememberPwd')
    if (name && pwd) {
        form.Name = name
        form.Pwd = pwd
        rememberMe.value = true
    }
})

function onLogin() {
    if (!formRef.value) return
    formRef.value.validate((valid) => {
        if (!valid) return
        loading.value = true
        axiosr.get('/api/Account/Login', {
            params: {
                Name: form.Name,
                Pwd: form.Pwd
            }
        }).then(res => {
            loading.value = false
            if (res.data.code === 200) {
                ElMessage.success('登录成功')
                localStorage.setItem("Uid", res.data.data.accMsg.id)
                localStorage.setItem("name", res.data.data.accMsg.name)
                localStorage.setItem("RoleId", res.data.data.accMsg.roleIds)
                localStorage.setItem("RoleName", res.data.data.accMsg.roleName)
                localStorage.setItem("token", res.data.data.toKen)
                // 记住密码逻辑
                if (rememberMe.value) {
                    localStorage.setItem('rememberName', form.Name)
                    localStorage.setItem('rememberPwd', form.Pwd)
                } else {
                    localStorage.removeItem('rememberName')
                    localStorage.removeItem('rememberPwd')
                }
                location.href = "/Menu"
                // 登录成功后的跳转逻辑
            }
            else {
                ElMessage.error(res.data.msg || '登录失败')
                refreshCaptcha()
            }
        }).catch(() => {
            loading.value = false
            ElMessage.error('网络错误')
            refreshCaptcha()
        })
    })
}
</script>

<style scoped>
:root {
    --primary-pink: #f9a8d4;
    --primary-pink-dark: #ec4899;
    --primary-pink-light: #fce7f3;
    --primary-pink-gradient: linear-gradient(120deg, #fce7f3 0%, #f9a8d4 100%);
    --card-bg: #fff;
    --card-radius: 28px;
    --card-shadow: 0 16px 48px rgba(249, 168, 212, 0.18);
    --input-border: #f9a8d4;
    --input-focus: #ec4899;
    --btn-gradient: linear-gradient(90deg, #3b82f6 0%, #60a5fa 100%);
    --btn-gradient-hover: linear-gradient(90deg, #2563eb 0%, #3b82f6 100%);
}

.login-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    background: var(--primary-pink-gradient);
    overflow: hidden;
}

.login-bg-svg {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    z-index: 0;
    pointer-events: none;
    opacity: 0.35;
}

.login-card {
    position: relative;
    z-index: 1;
    background: var(--card-bg);
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
    padding: 64px 56px 48px 56px;
    width: 420px;
    max-width: 96vw;
    transition: box-shadow 0.3s, transform 0.3s;
    display: flex;
    flex-direction: column;
    gap: 2.5rem;
}

.login-card:hover {
    box-shadow: 0 28px 80px rgba(249, 168, 212, 0.22);
    transform: translateY(-8px) scale(1.03);
}

.login-title {
    text-align: center;
    font-size: 2.4rem;
    font-weight: 900;
    color: var(--primary-pink-dark);
    margin-bottom: 0.5rem;
    letter-spacing: 2px;
    text-shadow: 0 2px 12px #fce7f3;
}

.login-form {
    margin-top: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.el-form-item {
    margin-bottom: 0;
}

.el-input__wrapper {
    border-radius: 12px !important;
    box-shadow: none !important;
    border: 1.5px solid var(--input-border) !important;
    transition: border-color 0.2s, box-shadow 0.2s;
    background: #fdf6fa;
    min-height: 48px;
}

.el-input__wrapper.is-focus {
    border-color: var(--input-focus) !important;
    box-shadow: 0 0 0 2px #fbcfe8 !important;
}

.el-input__inner::placeholder {
    color: #f9a8d4;
    opacity: 1;
}

.login-btn {
    width: 100%;
    font-size: 1.22rem;
    border-radius: 14px;
    height: 54px;
    background: var(--btn-gradient);
    border: none;
    box-shadow: 0 8px 28px rgba(60, 120, 200, 0.13);
    font-weight: 800;
    letter-spacing: 1px;
    transition: background 0.2s, box-shadow 0.2s, transform 0.1s;
    color: #fff;
}

.login-btn:hover {
    background: var(--btn-gradient-hover);
    box-shadow: 0 16px 40px rgba(60, 120, 200, 0.20);
    transform: translateY(-2px) scale(1.02);
}

.captcha-img {
    height: 48px;
    border-radius: 12px;
    cursor: pointer;
    border: 1.5px solid var(--input-border);
    background: linear-gradient(90deg, #fce7f3 0%, #fdf6fa 100%);
    transition: box-shadow 0.2s, border-color 0.2s, background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: bold;
    letter-spacing: 4px;
    user-select: none;
    color: var(--primary-pink-dark);
}

.captcha-img:hover {
    box-shadow: 0 2px 20px rgba(249, 168, 212, 0.20);
    border-color: var(--primary-pink-dark);
    background: linear-gradient(90deg, #fbcfe8 0%, #fce7f3 100%);
}

@media (max-width: 600px) {
    .login-card {
        padding: 28px 4vw 18px 4vw;
        width: 99vw;
        min-width: unset;
        gap: 1.2rem;
    }

    .login-title {
        font-size: 1.3rem;
    }

    .login-btn {
        font-size: 1rem;
        height: 44px;
    }

    .captcha-img {
        font-size: 1.1rem;
        height: 38px;
    }
}
</style>
